package cn.iocoder.yudao.module.exam.service.studentanswer;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.studentanswer.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.studentanswer.StudentAnswerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.studentanswer.StudentAnswerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.STUDENT_ANSWER_NOT_EXISTS;

/**
 * 考题管理 Service 实现类
 *
 * @author francis
 */
@Service
@Validated
public class StudentAnswerServiceImpl implements StudentAnswerService {

    @Resource
    private StudentAnswerMapper studentAnswerMapper;

    @Override
    public Long createStudentAnswer(StudentAnswerSaveReqVO createReqVO) {
        // 插入
        StudentAnswerDO studentAnswer = BeanUtils.toBean(createReqVO, StudentAnswerDO.class);
        studentAnswerMapper.insert(studentAnswer);

        // 返回
        return studentAnswer.getId();
    }

    @Override
    public void updateStudentAnswer(StudentAnswerSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentAnswerExists(updateReqVO.getId());
        // 更新
        StudentAnswerDO updateObj = BeanUtils.toBean(updateReqVO, StudentAnswerDO.class);
        studentAnswerMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentAnswer(Long id) {
        // 校验存在
        validateStudentAnswerExists(id);
        // 删除
        studentAnswerMapper.deleteById(id);
    }

    @Override
        public void deleteStudentAnswerListByIds(List<Long> ids) {
        // 删除
        studentAnswerMapper.deleteByIds(ids);
        }


    private void validateStudentAnswerExists(Long id) {
        if (studentAnswerMapper.selectById(id) == null) {
            throw exception(STUDENT_ANSWER_NOT_EXISTS);
        }
    }

    @Override
    public StudentAnswerDO getStudentAnswer(Long id) {
        return studentAnswerMapper.selectById(id);
    }

    @Override
    public PageResult<StudentAnswerDO> getStudentAnswerPage(StudentAnswerPageReqVO pageReqVO) {
        return studentAnswerMapper.selectPage(pageReqVO);
    }

}